# -*- coding:utf-8 -*-
"""
@Time : 2022/4/23 8:33 PM
@Author: binkuolo
@Des: views home
"""
import json
from datetime import datetime, timedelta

from fastapi import Request, APIRouter, Query
from fastapi.responses import HTMLResponse, JSONResponse

from fastapi.templating import Jinja2Templates

from models.models import Userposts, Post, User, Order, User_vip

templates = Jinja2Templates(directory="templates")
router = APIRouter()


@router.get("/", response_class=HTMLResponse)
async def index_template(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


@router.get("/od", response_class=HTMLResponse)
async def index_template(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})
@router.get("/guanhua", response_class=HTMLResponse)
async def index_template(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


@router.get("/detail/{id}", response_class=HTMLResponse)
async def detail_template(request: Request,
                          pid: str = Query(None),
                          trade_no: str = Query(None),
                          out_trade_no: str = Query(None),
                          type: str = Query(None),
                          name: str = Query(None),
                          money: float = Query(None),
                          trade_status: str = Query(None),
                          sign: str = Query(None),
                          sign_type: str = Query(None)):
    if pid is None:
        return templates.TemplateResponse("index.html", {"request": request})
    time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(out_trade_no, pid, trade_no, type, name, money, trade_status, sign, sign_type, '1111111')
    trade = await request.app.state.cache.get(out_trade_no)
    print(trade)
    if trade is None:
        return templates.TemplateResponse("index.html", {"request": request, 'error': '订单不存在'})
    trade_data = json.loads(trade)
    user = await User.get_or_none(id=trade_data['user'])
    if user is None:
        return templates.TemplateResponse("index.html", {"request": request, 'error': '用户不存在'})
    post = await Post.get_or_none(id=trade_data['posts'])

    await Userposts.create(price=money,
                           time=time, is_order=1,
                           user=user,
                           posts=post,
                           order_link=trade_data['order_link'], )
    return templates.TemplateResponse("index.html", {"request": request})


@router.get("/oredr/{id}", response_class=HTMLResponse)
async def detail_template(request: Request,
                          pid: str = Query(None),
                          trade_no: str = Query(None),
                          out_trade_no: str = Query(None),
                          type: str = Query(None),
                          name: str = Query(None),
                          money: float = Query(None),
                          trade_status: str = Query(None),
                          sign: str = Query(None),
                          sign_type: str = Query(None)):
    if pid is None:
        return templates.TemplateResponse("index.html", {"request": request})

    print(out_trade_no, pid, trade_no, type, name, money, trade_status, sign, sign_type, '1111111')
    trade = await request.app.state.cache.get(out_trade_no)
    print(trade)
    if trade is None:
        return templates.TemplateResponse("index.html", {"request": request, 'error': '订单不存在'})
    trade_data = json.loads(trade)
    user = await User.get_or_none(id=trade_data['user'])
    if user is None:
        return templates.TemplateResponse("index.html", {"request": request, 'error': '用户不存在'})
    order = await Order.get_or_none(id=trade_data['order'])
    time = datetime.now() + timedelta(days=order.times)
    user_vip = await User_vip.get_or_none(user=user, vip_type=order.id)
    if user_vip is not None:
        user_vip.vip_time = time + user_vip.vip_time
        await user_vip.save()
        return templates.TemplateResponse("index.html", {"request": request, 'error': '续费成功！'})

    await User_vip.create(vip_type=order.id,
                          vip_time=time,
                          user=user,
                          vip_status=1, )
    return templates.TemplateResponse("index.html", {"request": request})
